<template>
  <div>
    <div class="flex justify-between items-center pr-4 py-2 bg-white">
      <LayoutBreadcrumb class="grow" />
      <!--      <span>-->
      <!--        <span class="mr-4">显示设置:</span>-->
      <!--        <a-select ref="select" placeholder="请选择" v-model:value="selected" style="width: 200px">-->
      <!--          <a-select-option value="a">项目a</a-select-option>-->
      <!--          <a-select-option value="b">项目b</a-select-option>-->
      <!--          <a-select-option value="c">项目c</a-select-option>-->
      <!--          <a-select-option value="d">项目d</a-select-option>-->
      <!--        </a-select>-->

      <!--        <a-button type="primary">设置</a-button>-->
      <!--      </span>-->
    </div>
    <div class="mt-2 bg-white p-2">
      <this-search  ref="search" @project-change="projectChange" @action="action"/>
    </div>
    <div class="mt-2 px-4 pb-4 bg-white">
      <this-table ref="table" @handleTableChange = "handleTableChange"/>
    </div>

    <!-- <a-table bordered class="bg-white px-4" :dataSource="dataSource" :columns="columns">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'sensor'">
          <span class="">
            <a>1 通道-压力计</a>
          </span>
        </template>
      </template>
    </a-table> -->
  </div>
</template>
<script lang="ts">
import { defineComponent,onMounted} from 'vue';
import { LayoutBreadcrumb } from '../../../layouts/default/header/components';
import { ref } from 'vue';
import { DownloadOutlined } from '@ant-design/icons-vue';
import { CheckOutlined } from '@ant-design/icons-vue';
import thisSearch from './search.vue';
import thisTable from './table.vue';
import { defHttp } from '/@/utils/http/axios';
interface Project {
  id: number;
  name: string;
}
export default defineComponent({
  components: { LayoutBreadcrumb, DownloadOutlined, CheckOutlined, thisSearch, thisTable },
  setup() {
    const selected = ref();
    const projects = ref<[Project]>();
    const total = ref<number>(0);
    const search = ref();
    const table = ref();
    let rows = new Array(10).fill({
      project: 'XX项目',
      point: '监测点A',
      level: '一般',
      exceed: 12.34,
      created_at: '2022-12-31 11:23',
      sensor: '',
    });

    const getProjects = ()=> {
      defHttp
        .get({
          url: '/project/search',
          params: { pageIndex: 1, pageSize: 500, keyword: '' },
        })
        .then((res) => {
          projects.value = res.data.pageList;
          search.value.setProjects(projects.value );
          table.value.setProjects(projects.value);
        });
    }

    const projectChange = (projectId:number)=>{
      search.value.setDtu(projectId);
      defHttp
        .get({
          url: '/sensor/get-by-project-id',
          params: { projectId:projectId },
        })
        .then((res) => {
          table.value.setSensorMap(res.data? res.data:[]);
        });
    }

    const action = (p:any)=>{
      defHttp
        .post({
          url: '/data/get-page',
          params: p,
        })
        .then((res) => {
          total.value = res.data.total;
          table.value.setData(res.data.pageList,{current:p.pageIndex,pageSize:p.pageSize,total:total.value} );
        });
    }

    const handleTableChange = (val) =>{
      search.value.tableChange(val.current);
    }

    onMounted(() => {
      getProjects();
    });




    return {
      selected,
      search,
      table,
      handleTableChange,
      projectChange,
      action,
      dataSource: rows,
    };
  },
});
</script>
